package com.sxfq.loansupermarket.backsystem.modular.report.service.impl;

import com.sxfq.loansupermarket.backsystem.core.util.StringUtil;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.report.entity.SearchWholeHospitalInfo;
import com.sxfq.loansupermarket.backsystem.modular.report.entity.WholeHospitalInfo;
import com.sxfq.loansupermarket.backsystem.modular.report.service.IGetTotleDoctornewService;
import com.sxfq.loansupermarket.backsystem.modular.report.service.IGetTotlenewService;
import com.sxfq.loansupermarket.backsystem.modular.report.service.IWholeHospitalnewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class WholeHospitalnewsServiceImpl implements IWholeHospitalnewsService {
    @Autowired
    private IGetTotlenewService getTotlenewService;

    @Autowired
    private IGetTotleDoctornewService getTotleDoctornewService;

    @Autowired
    private SqlMapper sqlMapper;

    @Override
    public List<WholeHospitalInfo> getList(SearchWholeHospitalInfo searchInfo) {
        String sql = getTotlenewService.getTotleSqlDepartmentnew(searchInfo);
        List<WholeHospitalInfo> list = sqlMapper.selectList(sql, WholeHospitalInfo.class);
        return list;
    }

    @Override
    public List<WholeHospitalInfo> getOneChannelList(SearchWholeHospitalInfo searchInfo) {
        String arrivalStore = "";
        if (StringUtil.isNotEmpty(searchInfo.getArrivalStore()) && !"请选择".equals(searchInfo.getArrivalStore())) {
            String arrivalStore2[] = searchInfo.getArrivalStore().split(",");
            if (arrivalStore2.length == 1) {
                arrivalStore = arrivalStore2[0];
            } else {
                for (int j = 0; j < arrivalStore2.length; j++) {
                    if (j == (arrivalStore2.length - 1)) {
                        arrivalStore += "'" + arrivalStore2[j];
                    } else if (j == 0) {
                        arrivalStore += arrivalStore2[j] + "'" + ",";
                    } else {
                        arrivalStore += "'" + arrivalStore2[j] + "'" + ",";
                    }
                }
            }
        }
        String sqla = " ";
        if (!"ks".equals(searchInfo.getDimensionSelectionCustomerAll()) && StringUtil.isNotEmpty(searchInfo.getDimensionSelectionCustomerAll())) {
            sqla = " and a.treatment_unit='" + searchInfo.getDimensionSelectionCustomerAll() + "'";
        }
        String sql1 = " select a.customer_id as id,a.one_channel from customer_status_statistics a where a.time>='" + searchInfo.getStartTime() + "' " +
                "and a.time<='" + searchInfo.getEndTime() + "' and a.abbreviation in ('" + arrivalStore + "') " + sqla + " group by one_channel order by a.one_channel";
        List<SearchWholeHospitalInfo> list1 = sqlMapper.selectList(sql1, SearchWholeHospitalInfo.class);
        String sql = " ";
        if (list1.size() > 0) {
            for (int i = 0; i < list1.size(); i++) {
                String classification = list1.get(i).getOneChannel();
                String condition = "";
                if (classification == "" || classification == null) {
                    condition = "b.one_channel is null";
                } else {
                    condition = "b.one_channel='" + classification + "'";
                }
                sql += getTotlenewService.getsqlDepartmentnew(searchInfo, classification, condition,condition, condition, condition, i, list1);
            }
            sql += " union all ";
            sql += getTotlenewService.getTotleSqlDepartmentnew(searchInfo);
        } else {
            sql = getTotlenewService.getTotleSqlDepartmentnew(searchInfo);
        }
        List<WholeHospitalInfo> list = sqlMapper.selectList(sql, WholeHospitalInfo.class);
        return list;
    }

    @Override
    public List<WholeHospitalInfo> getOneMediumList(SearchWholeHospitalInfo searchInfo) {
        String arrivalStore = "";
        if (StringUtil.isNotEmpty(searchInfo.getArrivalStore()) && !"请选择".equals(searchInfo.getArrivalStore())) {
            String arrivalStore2[] = searchInfo.getArrivalStore().split(",");
            if (arrivalStore2.length == 1) {
                arrivalStore = arrivalStore2[0];
            } else {
                for (int j = 0; j < arrivalStore2.length; j++) {
                    if (j == (arrivalStore2.length - 1)) {
                        arrivalStore += "'" + arrivalStore2[j];
                    } else if (j == 0) {
                        arrivalStore += arrivalStore2[j] + "'" + ",";
                    } else {
                        arrivalStore += "'" + arrivalStore2[j] + "'" + ",";
                    }
                }
            }
        }
        String sqla = " ";
        if (!"ks".equals(searchInfo.getDimensionSelectionCustomerAll()) && StringUtil.isNotEmpty(searchInfo.getDimensionSelectionCustomerAll())) {
            sqla = " and a.treatment_unit='" + searchInfo.getDimensionSelectionCustomerAll() + "'";
        }
        String sql1 = " select a.customer_id as id,a.one_medium from customer_status_statistics a where a.time>='" + searchInfo.getStartTime() + "' " +
                "and a.time<='" + searchInfo.getEndTime() + "' and a.abbreviation in ('" + arrivalStore + "') " + sqla + " group by one_medium order by a.one_medium";
        List<SearchWholeHospitalInfo> list1 = sqlMapper.selectList(sql1, SearchWholeHospitalInfo.class);
        String sql = " ";
        if (list1.size() > 0) {
            for (int i = 0; i < list1.size(); i++) {
                String classification = list1.get(i).getOneMedium();
                String condition = "";
                if (classification == "" || classification == null) {
                    condition = "b.one_medium is null";
                } else {
                    condition = "b.one_medium='" + classification + "'";
                }
                sql += getTotlenewService.getsqlDepartmentnew(searchInfo, classification, condition,condition, condition, condition, i, list1);
            }
            sql += " union all ";
            sql += getTotlenewService.getTotleSqlDepartmentnew(searchInfo);
        } else {
            sql = getTotlenewService.getTotleSqlDepartmentnew(searchInfo);
        }
        List<WholeHospitalInfo> list = sqlMapper.selectList(sql, WholeHospitalInfo.class);
        return list;
    }

    @Override
    public List<WholeHospitalInfo> getCustomerAgeList(SearchWholeHospitalInfo searchInfo) {
        String arrivalStore = "";
        if (StringUtil.isNotEmpty(searchInfo.getArrivalStore()) && !"请选择".equals(searchInfo.getArrivalStore())) {
            String arrivalStore2[] = searchInfo.getArrivalStore().split(",");
            if (arrivalStore2.length == 1) {
                arrivalStore = arrivalStore2[0];
            } else {
                for (int j = 0; j < arrivalStore2.length; j++) {
                    if (j == (arrivalStore2.length - 1)) {
                        arrivalStore += "'" + arrivalStore2[j];
                    } else if (j == 0) {
                        arrivalStore += arrivalStore2[j] + "'" + ",";
                    } else {
                        arrivalStore += "'" + arrivalStore2[j] + "'" + ",";
                    }
                }
            }
        }
        String sqla = " ";
        if (!"ks".equals(searchInfo.getDimensionSelectionCustomerAll()) && StringUtil.isNotEmpty(searchInfo.getDimensionSelectionCustomerAll())) {
            sqla = " and a.treatment_unit='" + searchInfo.getDimensionSelectionCustomerAll() + "'";
        }
        String sql1 = " select a.customer_id as id,a.age_range from customer_status_statistics a where a.time>='" + searchInfo.getStartTime() + "' " +
                "and a.time<='" + searchInfo.getEndTime() + "' and a.abbreviation in ('" + arrivalStore + "') " + sqla + " group by age_range order by a.age_range";
        List<SearchWholeHospitalInfo> list1 = sqlMapper.selectList(sql1, SearchWholeHospitalInfo.class);
        String sql = "";
        if (list1.size() > 0) {
            for (int i = 0; i < list1.size(); i++) {
                String classification = list1.get(i).getAgeRange();
                String condition = "";
                if (classification == "" || classification == null) {
                    condition = "b.age_range is null";
                } else {
                    condition = "b.age_range='" + classification + "'";
                }
                sql += getTotlenewService.getsqlDepartmentnew(searchInfo, classification, condition,condition, condition, condition, i, list1);
            }
            sql += " union all ";
            sql += getTotlenewService.getTotleSqlDepartmentnew(searchInfo);
        } else {
            sql = getTotlenewService.getTotleSqlDepartmentnew(searchInfo);
        }

        List<WholeHospitalInfo> list = sqlMapper.selectList(sql, WholeHospitalInfo.class);
        return list;
    }

    @Override
    public List<WholeHospitalInfo> getTcNameList(SearchWholeHospitalInfo searchInfo) {
        String arrivalStore = "";
        if (StringUtil.isNotEmpty(searchInfo.getArrivalStore()) && !"请选择".equals(searchInfo.getArrivalStore())) {
            String arrivalStore2[] = searchInfo.getArrivalStore().split(",");
            if (arrivalStore2.length == 1) {
                arrivalStore = arrivalStore2[0];
            } else {
                for (int j = 0; j < arrivalStore2.length; j++) {
                    if (j == (arrivalStore2.length - 1)) {
                        arrivalStore += "'" + arrivalStore2[j];
                    } else if (j == 0) {
                        arrivalStore += arrivalStore2[j] + "'" + ",";
                    } else {
                        arrivalStore += "'" + arrivalStore2[j] + "'" + ",";
                    }
                }
            }
        }
        String sql1 = " ";
        if (!"ks".equals(searchInfo.getDimensionSelectionCustomerAll()) && StringUtil.isNotEmpty(searchInfo.getDimensionSelectionCustomerAll())) {
            sql1 = " select a.id,a.tc_name from hospital_section_statistics a where a.time>='" + searchInfo.getStartTime() + "' " +
                    "and a.time<='" + searchInfo.getEndTime() + "' and a.abbreviation in ('" + arrivalStore + "') group by tc_name order by a.tc_name";
        } else {
            sql1 = " select * from (select a.customer_id as id,a.tc_name from customer_status_statistics a where a.time>='" + searchInfo.getStartTime() + "' " +
                    " and a.time<='" + searchInfo.getEndTime() + "' and a.abbreviation in ('" + arrivalStore + "') " +
                    " union all" +
                    " select a.id as id,a.tc_name from hospital_section_statistics a where a.time>='" + searchInfo.getStartTime() + "' " +
                    " and a.time<='" + searchInfo.getEndTime() + "' and a.abbreviation in ('" + arrivalStore + "'))t" +
                    " group by t.tc_name order by t.tc_name";
        }
        List<SearchWholeHospitalInfo> list1 = sqlMapper.selectList(sql1, SearchWholeHospitalInfo.class);
        String sql = "";
        if (list1.size() > 0) {
            for (int i = 0; i < list1.size(); i++) {
                String classification = list1.get(i).getTcName();
                String condition = "";
                String condition1 = "";
                String conditionOne = "";
                String conditionTwo = "";
                if (classification == "" || classification == null) {
                    condition = "b.tc_name is null";
                    condition1 = "c.tc_name is null";
                } else {
                    condition = "b.tc_name='" + classification + "'";
                    condition1 = "c.tc_name='" + classification + "'";
                }
                sql += getTotlenewService.getsqlDepartmentnew(searchInfo, classification, condition, condition1, conditionOne, conditionTwo, i, list1);
            }
            sql += " union all ";
            sql += getTotlenewService.getTotleSqlDepartmentnew(searchInfo);
        } else {
            sql = getTotlenewService.getTotleSqlDepartmentnew(searchInfo);
        }
        List<WholeHospitalInfo> list = sqlMapper.selectList(sql, WholeHospitalInfo.class);
        return list;
    }

    @Override
    public List<WholeHospitalInfo> getDoctorList(SearchWholeHospitalInfo searchInfo) {
        String arrivalStore = "";
        if (StringUtil.isNotEmpty(searchInfo.getArrivalStore()) && !"请选择".equals(searchInfo.getArrivalStore())) {
            String arrivalStore2[] = searchInfo.getArrivalStore().split(",");
            if (arrivalStore2.length == 1) {
                arrivalStore = arrivalStore2[0];
            } else {
                for (int j = 0; j < arrivalStore2.length; j++) {
                    if (j == (arrivalStore2.length - 1)) {
                        arrivalStore += "'" + arrivalStore2[j];
                    } else if (j == 0) {
                        arrivalStore += arrivalStore2[j] + "'" + ",";
                    } else {
                        arrivalStore += "'" + arrivalStore2[j] + "'" + ",";
                    }
                }
            }
        }
        String sql1 = " ";
        sql1 = " select a.id,a.doctor from hospital_section_statistics a where a.time>='" + searchInfo.getStartTime() + "' " +
                "and a.time<='" + searchInfo.getEndTime() + "' and a.abbreviation in ('" + arrivalStore + "') group by doctor order by a.doctor";
        List<SearchWholeHospitalInfo> list1 = sqlMapper.selectList(sql1, SearchWholeHospitalInfo.class);
        String sql = "";
        if (list1.size() > 0) {
            for (int i = 0; i < list1.size(); i++) {
                String classification = list1.get(i).getDoctor();
                String condition = "";
                if (classification == "" || classification == null) {
                    condition = "c.doctor is null";
                } else {
                    condition = "c.doctor='" + classification + "'";
                }
                sql += getTotleDoctornewService.getsqlDepartmentnew(searchInfo, classification, condition, i, list1);
            }
            sql += " union all ";
            sql += getTotlenewService.getTotleSqlDepartmentnew(searchInfo);
        } else {
            sql += getTotlenewService.getTotleSqlDepartmentnew(searchInfo);
        }
        List<WholeHospitalInfo> list = sqlMapper.selectList(sql, WholeHospitalInfo.class);
        return list;
    }
}